package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ToolUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.domain.SysGoodsOrder;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysUserMoney;
import com.ruoyi.project.system.service.ISysGoodsOrderService;
import com.ruoyi.project.system.service.ISysGoodsService;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysUserMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 二手物品订单Controller
 *
 * @author jjp
 * @date 2020-04-24
 */
@RestController
@Api(description = "二手物品订单Controller")
@RequestMapping("/mobileUser/goodsOrder")
public class UserGoodsOrderController extends BaseController {
    @Autowired
    private ISysGoodsOrderService sysGoodsOrderService;
    @Autowired
    private ISysUserMoneyService sysUserMoneyService;
    @Autowired
    private ISysGoodsService sysGoodsService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;

    /**
     * 查询二手物品订单列表
     */
    @ApiOperation("查询二手物品订单列表")
    @SignatureValidation
    @GetMapping("/list")
    public TableDataInfo list(SysGoodsOrder sysGoodsOrder) {
        sysGoodsOrder.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        startPage();
        List<SysGoodsOrder> list = sysGoodsOrderService.selectSysGoodsOrderList(sysGoodsOrder);
        return getDataTable(list);
    }


    /**
     * 获取二手物品订单详细信息
     */
    @ApiOperation("获取二手物品订单详细信息")
    @SignatureValidation
    @GetMapping(value = "/{goodsOrderId}")
    public AjaxResult getInfo(@PathVariable("goodsOrderId") Long goodsOrderId) {
        return AjaxResult.success(sysGoodsOrderService.selectSysGoodsOrderById(goodsOrderId));
    }

    /**
     * 删除二手物品订单
     */
    @ApiOperation("删除二手物品订单")
    @Log(title = "二手物品订单", businessType = BusinessType.DELETE)
    @SignatureValidation
    @DeleteMapping("/{goodsOrderIds}")
    public AjaxResult remove(@PathVariable Long[] goodsOrderIds) {
        return toAjax(sysGoodsOrderService.deleteSysGoodsOrderByIds(goodsOrderIds));
    }

    /**
     * 确认收货
     */
    @ApiOperation("确认收货")
    @Log(title = "二手物品订单", businessType = BusinessType.UPDATE)
    @SignatureValidation
    @RepeatSubmit
    @Transactional
    @PutMapping("/updataGoods")
    public AjaxResult updataGoods(@RequestBody SysGoodsOrder sysGoodsOrder) {
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysGoodsOrder.setMobileUserId(mobileUserId);
        //确认收货
        sysGoodsOrder.setGoodsOrderStatus("5");


        Long goodsId = sysGoodsOrder.getGoodsId();
        SysGoods sysGoods = sysGoodsService.selectSysGoodsById(goodsId);
        Long mobileUserId1 = sysGoods.getMobileUserId();
        SysMobileUser sysMobileUser1 = sysMobileUserService.selectSysMobileUserById(mobileUserId1);
        SysMobileUser sysMobileUser = new SysMobileUser();

        sysMobileUser.setMobileUserId(mobileUserId1);
        sysMobileUser.setMoney(ToolUtil.add(sysMobileUser1.getMoney(), sysGoods.getMoney()));
        sysMobileUserService.updateSysMobileUser(sysMobileUser);

        SysUserMoney sysUserMoney = new SysUserMoney();
        sysUserMoney.setMobileUserId(mobileUserId1);
        sysUserMoney.setGoodsOrderId(sysGoodsOrder.getGoodsOrderId());
        sysUserMoney.setMoney(sysGoodsOrder.getMoney());
        sysUserMoney.setMoneyType("0");


        int i = sysGoodsOrderService.updateSysGoodsOrder(sysGoodsOrder);
        if (i == 1) {
            sysUserMoneyService.insertSysUserMoney(sysUserMoney);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    /**
     * 没有发货，退款
     */
    @ApiOperation("没有发货，退款")
    @SignatureValidation
    @RepeatSubmit
    @Transactional
    @PutMapping("/returnMoneyGoods")
    public AjaxResult returnMoneyGoods(@RequestBody SysGoodsOrder sysGoodsOrder) {
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysGoodsOrder.setMobileUserId(mobileUserId);
        //确认收货
        sysGoodsOrder.setGoodsOrderStatus("6");
        SysUserMoney sysUserMoney = new SysUserMoney();
        sysUserMoney.setMobileUserId(mobileUserId);
        sysUserMoney.setGoodsOrderId(sysGoodsOrder.getGoodsOrderId());
        sysUserMoney.setMoney(sysGoodsOrder.getMoney());
        sysUserMoney.setMoneyType("2");

        int i = sysGoodsOrderService.updateSysGoodsOrder(sysGoodsOrder);
        if (i == 1) {
            sysUserMoneyService.insertSysUserMoney(sysUserMoney);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    /**
     * 退货退款
     */
    @ApiOperation("退货退款")
    @SignatureValidation
    @RepeatSubmit
    @Transactional
    @PutMapping("/returnGoods")
    public AjaxResult returnGoods(@RequestBody SysGoodsOrder sysGoodsOrder) {
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysGoodsOrder.setMobileUserId(mobileUserId);
        //确认收货
        sysGoodsOrder.setGoodsOrderStatus("7");
        SysUserMoney sysUserMoney = new SysUserMoney();
        sysUserMoney.setMobileUserId(mobileUserId);
        sysUserMoney.setGoodsOrderId(sysGoodsOrder.getGoodsOrderId());
        sysUserMoney.setMoney(sysGoodsOrder.getMoney());
        sysUserMoney.setMoneyType("2");

        int i = sysGoodsOrderService.updateSysGoodsOrder(sysGoodsOrder);
        if (i == 1) {
            sysUserMoneyService.insertSysUserMoney(sysUserMoney);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


}
